goog.provide('dkpoints.nav.AuthHandler');

goog.require('dkpoints.auth.AuthState');
goog.require('dkpoints.utils');



/**
 * AuthHandler is responsible for handling all navigation events for /#/auth/
 * @param {dkpoints.ServiceProvider} sp The ServiceProvider object with all the
 *    cool dependencies baked in.
 * @constructor
 * @implements {dkpoints.nav.Handler}
 */
dkpoints.nav.AuthHandler = function(sp) {
  //auth, authStatus, baseURL

  /**
   * @type {string}
   * @private
   */
  this.baseURL_ = sp.getBaseURL();

  /**
   * @type {dkpoints.auth.Auth}
   * @private
   */
  this.auth_ = sp.getAuth();

  /**
   * @type {dkpoints.nav.Nav}
   * @private
   */
  this.navManager_ = sp.getNavManager();
};


/**
 * The auth level required for this module (no authorization required)
 * @type {dkpoints.auth.AuthState}
 * @const
 */
dkpoints.nav.AuthHandler.requiredAuth = dkpoints.auth.AuthState.NO_AUTH;


/**
 * Handle the authentication event.
 * @param {dkpoints.nav.Path} path The path for this request.
 */
dkpoints.nav.AuthHandler.prototype.handle = function(path) {
  var referer = '';

  /**
   * @type {?string}
   * @private
   */
  this.referer_ = referer = (this.navManager_.getReferer());

  var token = path.params;

  if (token === 'login') {
    this.auth_.login(this.baseURL_, referer);
  }
  else if (token === 'logout') {
    var logoutCallback = goog.bind(this.logoutComplete, this);
    this.auth_.logout(logoutCallback);
  }
};


/**
 * If the auth handler is the current handler, then we don't need to worry about
 * transitioning from one state to another.  Just call handle.
 * @param {dkpoints.nav.Path} path An object with request-specific parameters.
 * @param {function()} onCancel Callback to be invoked if user decides
 *    not to navigate away from what they're doing.
 */
dkpoints.nav.AuthHandler.prototype.transition = function(path, onCancel) {
  this.handle(path);
};


/**
 * When the logout is complete and the new auth status is set, redirect back
 * to the previous location.
 */
dkpoints.nav.AuthHandler.prototype.logoutComplete = function() {
  /*
   * There may be a race condition here where the user may navigate to another
   * page while waiting for the logout response, causing this.dispose() to have
   * cleared this.referer_. If this is the case, just do nothing, as the user
   * has already navigated to wherever they want to be.
   */
  var referer = this.referer_;
  if (! goog.isNull(referer)) {
    dkpoints.utils.setLocationHash(referer);
  }
};


/**
 * Save state and clear the user-interface so the next handler can use the
 * screen.
 * @param {function(boolean)} onExit Callback once exit is either
 *    complete or cancelled by the user.  Passing true to the callback means
 *    that the handler has relinquished control of thep age; false means
 *    the user opted not to navigate away.
 */
dkpoints.nav.AuthHandler.prototype.exit = function(onExit) {
  /**
   * Clear on dispose to prevent redirecting to an incorrect referer later on.
   * @type {?string}
   * @private
   */
  this.referer_ = null;

  onExit(true);
};
